﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;


namespace SensorNetworkUI
{
    public class UserDataStructure
    {
        int userid;

        public int Userid
        {
            get { return userid; }
            set { userid = value; }
        }

        public UserDataStructure(String name)
        {

            this.Uname = name;
        }
        string _uname;

        public string Uname
        {
            get { return _uname; }
            set { _uname = value; }
        }
        string _password;

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
        UserType type;

        public UserType Type
        {
            get { return type; }
            set { type = value; }
        }


        List<NetworkDataStructure> _allowedNetworks = new List<NetworkDataStructure>();
        public bool GetNetworks()
        {
            SqlConnection conn = new SqlConnection(Constants.ConnectionString);
            SqlCommand comm = conn.CreateCommand();
            string query = "select * from   user_network, [user],network where [dbo].[user].username = @username and [USER].userid = user_network.user_id and network.network_id = user_network.network_id";
            comm.CommandText = query;
            conn.Open();
            comm.Parameters.AddWithValue("@username", this.Uname);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {


                NetworkDataStructure ds = new NetworkDataStructure();
                ds.Name = read["network_name"].ToString();
                ds.Id = int.Parse(read["network_id"].ToString());

                _allowedNetworks.Add(ds);
            }
            conn.Close();
            return true;


        }
        public bool PopulateNetworks()
        {
            foreach (NetworkDataStructure str in _allowedNetworks)
            {
                str.Fetch();
                str.PopulateSensors();

            }


            return true;

        }

        public bool UpdateNetworkList()
        {
            SqlConnection conn = new SqlConnection(Constants.ConnectionString);
            conn.Open();
            SqlCommand comm = conn.CreateCommand();
                comm.CommandText = "delete from user_network where user_id = @userid";
                comm.Parameters.AddWithValue("@userid", Userid.ToString());
                comm.ExecuteNonQuery();
                int i = 0;
            foreach (NetworkDataStructure net in _allowedNetworks)
            {

                comm.CommandText = "insert into user_network values("+ net.Id+","+Userid+")";
                //comm.Parameters.AddWithValue("@network_id", net.Id);
                //comm.Parameters.AddWithValue("@userid", userid);
                comm.ExecuteNonQuery();



            }
            conn.Close();


            return true;
        }


        public List<NetworkDataStructure> AllowedNetworks
        {
            get { return _allowedNetworks; }
            set { _allowedNetworks = value; }
        }

        String _nickname = "";

        public String Nickname
        {
            get { return _nickname; }
            set { _nickname = value; }
        }

        public override string ToString()
        {
            return this.Uname+" ( "+this.Nickname+" ) ";
        }
        public override bool Equals(object obj)
        {
            return (this.Uname.Equals(((UserDataStructure)obj).Uname));
        }
        public override int GetHashCode()
        {
            return Uname.GetHashCode();
        }
    }
}